<%@ page language="java" import="com.alibaba.fastjson.JSONObject" pageEncoding="UTF-8" %>
<%@ page import="weaver.common.DateUtil" %>
<%@ page import="weaver.conn.RecordSet" %>
<%@ page import="weaver.general.BaseBean" %>
<%@ page import="weaver.general.Util" %>
<%@ page import="weaver.hrm.HrmUserVarify" %>
<%@ page import="weaver.hrm.User" %>
<%@ page import="java.math.BigDecimal" %>
<%@ page import="java.math.RoundingMode" %>
<%@ page import="java.time.LocalDate" %>
<%@ page import="java.time.format.DateTimeFormatter" %>
<%--获取流水号--%>
<%
    JSONObject dataJson = new JSONObject();
    User user = HrmUserVarify.getUser(request, response);
    int userid = user.getUID();
    try {
        rs.executeQuery("select count(id) from uf_zyxm  where instr(ry," + userid + ") > 0");
        rs.next();
        int counts = Util.getIntValue(rs.getString(1));
        dataJson.put("isAllow", counts > 0?3:1);
        dataJson.put("zyxmData", getZyxm());

        String year = DateUtil.getYear();
        String dateString = DateUtil.getCurrentDate();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDate date = LocalDate.parse(dateString, formatter);
        String quarterStart = getQuarterStartDate(date).toString();
        String quarterEnd = getQuarterEndDate(date).toString();
        //年度投资计划
        JSONObject ndtzjhJson = new JSONObject();
        ndtzjhJson.put("tzjhje", getTzjhAmount(year));
        ndtzjhJson.put("gdzcxm", getGdzcCount(year));
        ndtzjhJson.put("gqtzxm", getGqtzCount(year));
        ndtzjhJson.put("jjtzxm", getJjtzCount(year));
        ndtzjhJson.put("bjdtz", getBjdtzCount(quarterStart, quarterEnd));
        dataJson.put("ndtzjhData", ndtzjhJson);
        //年度投资进展
        JSONObject ndtzjzJson = new JSONObject();
        ndtzjzJson.put("ljwcje", getLjwcAmount(year));
        ndtzjzJson.put("xmlx", getXmllCount(year));
        ndtzjzJson.put("twhsy", getTwhsyCount(year));
        ndtzjzJson.put("tzjc", getTzjcCount(year));
        dataJson.put("ndtzjzData", ndtzjzJson);
        //重大产业投项目
        JSONObject zdcytxxmJson = new JSONObject();
        double ndjhtz = getNdtzAmount(year);
        double ljwctz = getZdcyLjwcAmount(year);
        double mbdzl =0.0;
        if(ndjhtz>0){
            mbdzl = roundToTwoDecimals((ljwctz / ndjhtz) * 100);
        }
        zdcytxxmJson.put("ndjhtz", ndjhtz);
        zdcytxxmJson.put("jhtzxm", getJhtzCount(year));
        zdcytxxmJson.put("ljwctz", ljwctz);
        zdcytxxmJson.put("mbdzl", mbdzl);
        // zdcytxxmJson.put("xxmkgl", roundToTwoDecimals(getXxmkgl(year)));
        zdcytxxmJson.put("xxmkgl", "100");
        dataJson.put("zdcytxxmData", zdcytxxmJson);
    }catch (Exception e){
        e.printStackTrace();
    }
    out.print(dataJson);
%>
<%!
    private static BaseBean baseBean = new BaseBean();
    private static RecordSet rs = new RecordSet();

    private JSONObject getZyxm() {
        JSONObject dataJson = new JSONObject();
        rs.executeQuery("select ydxm,ljdje,byxzxm,cxtjxm,ljztxm from uf_zyxm  where 1=1");
        rs.next();
        dataJson.put("ydxm", Util.null2String(rs.getString("ydxm")));
        dataJson.put("ljdje", Util.null2String(rs.getString("ljdje")));
        dataJson.put("byxzxm", Util.null2String(rs.getString("byxzxm")));
        dataJson.put("cxtjxm", Util.null2String(rs.getString("cxtjxm")));
        dataJson.put("ljztxm", Util.null2String(rs.getString("ljztxm")));
        return dataJson;
    }

    //计划投资金额
    private double getTzjhAmount(String year) {
        rs.executeQuery("select sum(a.amount) from (" +
                " select (sum(bndbqyjhtzewy)/10000) amount from uf_LHRCDUHIgdzctzjh where n='" + year + "' " +
                " union all " +
                " select (sum(bndbqyjhtzewy)/10000) amount from uf_LHRDLI5Vgqtzjh where n='" + year + "' " +
                " union all " +
                " select (sum(bndbqyjhtzewy)/10000) amount from uf_LHREAW6Ijjtzjh where n='" + year + "' " +
                ") a");
        rs.next();
        return roundToTwoDecimals(Util.getDoubleValue(rs.getString(1), 0));
    }

    //固定资产项目
    private int getGdzcCount(String year) {
        rs.executeQuery("select count(id) from uf_LHRCDUHIgdzctzjh where n=?", year);
        rs.next();
        return Util.getIntValue(rs.getString(1), 0);
    }

    //股权投资项目
    private int getGqtzCount(String year) {
        rs.executeQuery("select count(id) from uf_LHRDLI5Vgqtzjh where n=?", year);
        rs.next();
        return Util.getIntValue(rs.getString(1), 0);
    }

    //基金投资项目
    private int getJjtzCount(String year) {
        rs.executeQuery("select count(id) from uf_LHREAW6Ijjtzjh where n=?", year);
        rs.next();
        return Util.getIntValue(rs.getString(1), 0);
    }

    //本季度调整
    private int getBjdtzCount(String begDate, String endDate) {
        rs.executeQuery("select count(t1.id) from (select a.id from formtable_main_656 a join workflow_requestbase b on a.requestid=b.requestid " +
                "where b. currentnodetype=3 and (a.gdrq>='" + begDate + "' and a.gdrq<='" + endDate + "') ) t " +
                "join formtable_main_656_dt1 t1 on t.id=t1.mainid where t1.dzlx=0");
        rs.next();
        int count1 = Util.getIntValue(rs.getString(1), 0);
        rs.executeQuery("select count(t1.id) from (select a.id from formtable_main_656 a join workflow_requestbase b on a.requestid=b.requestid " +
                "where b. currentnodetype=3 and (a.gdrq>='" + begDate + "' and a.gdrq<='" + endDate + "') ) t " +
                "join formtable_main_656_dt2 t1 on t.id=t1.mainid where t1.dzlx=0");
        rs.next();
        int count2 = Util.getIntValue(rs.getString(1), 0);
        rs.executeQuery("select count(t1.id) from (select a.id from formtable_main_656 a join workflow_requestbase b on a.requestid=b.requestid " +
                "where b. currentnodetype=3 and (a.gdrq>='" + begDate + "' and a.gdrq<='" + endDate + "') ) t " +
                "join formtable_main_656_dt3 t1 on t.id=t1.mainid where t1.dzlx=0");
        rs.next();
        int count3 = Util.getIntValue(rs.getString(1), 0);
        return count1 + count2 + count3;
    }

    //年度投资进展：累计完成金额
    private double getLjwcAmount(String year) {
        RecordSet rs = new RecordSet();

        rs.executeQuery("select sum(bjwctzewy)/10000 from uf_gqjdtzxmqk where n=?", year);
        rs.next();
        double count1 = roundToTwoDecimals(Util.getDoubleValue(rs.getString(1), 0));
        rs.executeQuery("select sum(bjwctzewy)/10000 from uf_jjjdtzxmqk where n=?", year);
        rs.next();
        double count2 = roundToTwoDecimals(Util.getDoubleValue(rs.getString(1), 0));
        rs.executeQuery("select sum(bjwctzewy)/10000 from uf_jdtzxmqkb where n=?", year);
        rs.next();
        double count3 = roundToTwoDecimals(Util.getDoubleValue(rs.getString(1), 0));
        return count1 + count2 + count3;
    }

    //项目立项（）--取表内当前年的zt=0为项目个数
    private int getXmllCount(String year) {
        rs.executeQuery("select sum(a.counts) from (" +
                " select count(id) counts from uf_LHRCDUHIgdzctzjh where n='" + year + "' and zt=0 " +
                " union all " +
                " select count(id) counts from uf_LHRDLI5Vgqtzjh where n='" + year + "' and zt=0 " +
                " union all " +
                " select count(id) counts from uf_LHREAW6Ijjtzjh where n='" + year + "' and zt=0 " +
                ") a");
        rs.next();
        return Util.getIntValue(rs.getString(1), 0);
    }

    //投委会审议（）个--取表内当前年的zt=1为项目个数
    private int getTwhsyCount(String year) {
        rs.executeQuery("select sum(a.counts) from (" +
                " select count(id) counts from uf_LHRCDUHIgdzctzjh where n='" + year + "' and zt=1 " +
                " union all " +
                " select count(id) counts from uf_LHRDLI5Vgqtzjh where n='" + year + "' and zt=1 " +
                " union all " +
                " select count(id) counts from uf_LHREAW6Ijjtzjh where n='" + year + "' and zt=1 " +
                ") a");
        rs.next();
        return Util.getIntValue(rs.getString(1), 0);
    }

    //投资决策（）个--取表内当前年的zt=2为项目个数
    private int getTzjcCount(String year) {
        rs.executeQuery("select sum(a.counts) from (" +
                " select count(id) counts from uf_LHRCDUHIgdzctzjh where n='" + year + "' and zt=2 " +
                " union all " +
                " select count(id) counts from uf_LHRDLI5Vgqtzjh where n='" + year + "' and zt=2 " +
                " union all " +
                " select count(id) counts from uf_LHREAW6Ijjtzjh where n='" + year + "' and zt=2 " +
                ") a");
        rs.next();
        return Util.getIntValue(rs.getString(1), 0);
    }

    //年度计划投资
    private double getNdtzAmount(String year) {
        RecordSet rs = new RecordSet();
        rs.executeQuery("select sum(ndtz)/10000 from uf_ydjhjtzdtz where n=? and (sfgua !=0 or sfgua is null)", year);
        rs.next();
        return roundToTwoDecimals(Util.getDoubleValue(rs.getString(1), 0));
    }

    //计划投资项目
    private int getJhtzCount(String year) {
        rs.executeQuery("select count(id) from uf_ydjhjtzdtz where n=? and (sfgua !=0 or sfgua is null)", year);
        rs.next();
        return Util.getIntValue(rs.getString(1), 0);
    }

    //累计完成投资
    private double getZdcyLjwcAmount(String year) {
        RecordSet rs = new RecordSet();
        double sum = 0.00;
        rs.executeQuery("select (sum(ndljwce)/10000) ndljwce,xmmc from uf_ydjhzdtzxmtz " +
                "where xmmc in (select id from uf_ydjhjtzdtz where sfgua !=0 or sfgua is null) and n=?" +
                " and y=(select max(y) from uf_ydjhzdtzxmtz t where t.xmmc=xmmc) " +
                "group by xmmc",year);
        while (rs.next()){
            sum += Util.getDoubleValue(rs.getString("ndljwce"), 0);
        }
        return roundToTwoDecimals(sum);
    }
    //新项目开工率
    private double getXxmkgl(String year) {
        double sum = 0.00;
        rs.executeQuery("select count(id) from uf_ydjhjtzdtz where jzqk=1 and n=?", year);
        rs.next();
        double count = roundToTwoDecimals(Util.getDoubleValue(rs.getString(1), 0));
        rs.executeQuery("select count(id) from uf_ydjhjtzdtz where n=?", year);
        rs.next();
        double counts = roundToTwoDecimals(Util.getDoubleValue(rs.getString(1), 0));
        if(count>0 && counts>0){
            sum =(count/counts)*100;
        }
        return sum;
    }

    private static LocalDate getQuarterStartDate(LocalDate date) {
        int monthValue = date.getMonthValue();
        int year = date.getYear();
        int quarterStartMonth = (monthValue - 1) / 3 * 3 + 1;
        return LocalDate.of(year, quarterStartMonth, 1);
    }

    private static LocalDate getQuarterEndDate(LocalDate date) {
        LocalDate quarterStart = getQuarterStartDate(date);
        LocalDate quarterEnd = quarterStart.plusMonths(3).minusDays(1);
        // Adjust for February in a leap year
        if (quarterEnd.getMonth() == java.time.Month.FEBRUARY && quarterEnd.getDayOfMonth() > 28) {
            quarterEnd = quarterEnd.minusDays(1);
        }
        return quarterEnd;
    }
    public static double roundToTwoDecimals(double value) {
        if (value == 0) {
            return 0;
        }
        return new BigDecimal(Double.toString(value)).setScale(2, RoundingMode.HALF_UP).doubleValue();
    }
%>